package com.tianqu.tms.core.taglib;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;

import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.TmsConstants;

/**
 * 根据menu选择生成右侧main列表
 * @author liugang
 *
 */
public class MainTag extends MenuTagSupport {

	private static final long serialVersionUID = -3879010825316012500L;
	//换行代码
	protected static final String CRLF = System.getProperty("line.separator");

	private String funcId;
	private MenuBean rootMenuBean;
	private MenuBean currentMenuBean;
	
	/**
	 * tag 处理开始
	 */
	public int doStartTag() throws JspException {
		// zhk liu.xingya start
//		rootMenuBean =(MenuBean) pageContext.getServletContext().getAttribute(TmsConstants.MENU_VO_KEY);
		rootMenuBean = (MenuBean) pageContext.getRequest().getAttribute(TmsConstants.MENU_VO_KEY);
		if (rootMenuBean != null) {
			funcId = pageContext.getRequest().getParameter("funcId");
		} else {
			rootMenuBean = (MenuBean) pageContext.getServletContext().getAttribute(TmsConstants.MENU_VO_KEY);
		}
		// zhk liu.xingya end
		Locale currentLoc = (Locale)pageContext.getSession().getAttribute(LOCALE_KEY);
		ResourceBundle res = ResourceBundle.getBundle(MENURESOURCE, currentLoc);
        if (funcId == null) {
			throw new JspTagException("funcId is required");
		}
		if (rootMenuBean == null) {
			throw new JspTagException("Cannot find MenuBean");
		}
		JspWriter out = null;
		try {
			out = pageContext.getOut(); 
			out.print(this.buildHtml(rootMenuBean, res));
		}catch(IOException e){
			throw new JspException("across:" + e.toString());
		}
        return EVAL_BODY_INCLUDE;
	}
	/**
	 * 
	 */
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	/**
	 * HTML生成
	 * <tr>
	        <td width="40%"><a class="action-link" href="#">待检测</a></td>
	        <td>【说明】该区域是用于什么作用的文本说明性信息</td>
	    </tr>
	    <tr class="gray">
	        <td class="60%"><a class="action-link" href="#">待确认</a></td>
	        <td>【说明】该区域是用于什么作用的文本说明性信息</td>
	    </tr>
	 * @param MenuBean
	 * @param res
	 * @return
	 * @throws JspException
	 */
	protected String buildHtml(MenuBean MenuBean, ResourceBundle res) throws JspException {

		currentMenuBean = MenuBean.getMenuVO(this.funcId);
		StringBuffer sb = new StringBuffer();
		if (currentMenuBean != null) {
			sb.append("<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">").append(CRLF);
			List beanList = new ArrayList();
			createBeanList(currentMenuBean, beanList);
			this.buildMenus(beanList, sb, res);
			sb.append("</table>").append(CRLF);
		}
		return sb.toString();
	}

	/**
	 * menubean list 的生成
	 * @param MenuBean
	 * @param beanList
	 * @return
	 * @throws JspException
	 */
	protected boolean createBeanList(MenuBean MenuBean, List beanList) throws JspException {

		try {
			List childList = MenuBean.getChildrenList();
			if(childList != null && childList.size() > 0){
				Iterator iterator = MenuBean.getChildrenList().iterator();
				while(iterator.hasNext()){
					MenuBean targetBean = (MenuBean)iterator.next();
					if (targetBean == null) {
						continue;
					}
					else {
						beanList.add(targetBean);
					}
				}
				return true;
			}else{
				return false;
			}
		}
		catch (Exception e) {
			throw new JspException(e.getMessage());
		}
	}

	/**
	 * menu html的生成
	 * @param beanList
	 * @param sb
	 * @param res
	 * @throws JspException
	 */
	protected void buildMenus(List beanList, StringBuffer sb, ResourceBundle res) throws JspException {

		int num = 0;
		for (Iterator iterator = beanList.iterator(); iterator.hasNext();) {
			MenuBean MenuBean = (MenuBean) iterator.next();
			if (MenuBean.getDescription().trim().equals("")) {
				continue;
			}
			if(super.hasViewCompetence(MenuBean)){
				num++;
				if(num%2 == 0){
					sb.append("<tr class=\"gray\">");
					sb.append("<td width=\"40%\"><a class=\"action-link\" href=\"");
				} else {
					sb.append("<tr>");
					sb.append("<td width=\"40%\"><a class=\"action-link\" href=\"");
				}
				sb.append(Registry.getConfig(TmsConstants.CONTEXT_ROOT_KEY));
				sb.append("/");
				sb.append(MenuBean.getTargetUri());
				sb.append("\">");
				sb.append(res.getString(MenuBean.getFunctionId() + ".menu.name"));
				sb.append("</a></td>");
				sb.append("<td>");
				sb.append(res.getString(MenuBean.getFunctionId() + ".menu.description"));
				sb.append("</td>");
				sb.append("</tr>");
			}
		}

	}
	public String getFuncId() {
		return funcId;
	}
	public void setFuncId(String funcId) {
		this.funcId = funcId;
	}
}
